package com.usbserial.scale;

import com.usbserial.scale.ScaleData.ScaleStatus;

/**
 * Domain object representing parsed weight information from a scale payload.
 */
public final class ScaleData {

    private final double weight;
    private final WeightUnit unit;
    private final ScaleStatus status;
    private final long timestampMillis;

    public ScaleData(double weight, WeightUnit unit, ScaleStatus status) {
        this.weight = weight;
        this.unit = unit;
        this.status = status;
        this.timestampMillis = System.currentTimeMillis();
    }

    public double getWeight() {
        return weight;
    }

    public WeightUnit getUnit() {
        return unit;
    }

    public ScaleStatus getStatus() {
        return status;
    }

    public long getTimestampMillis() {
        return timestampMillis;
    }

    public double getWeightInGrams() {
        return unit.toGrams(weight);
    }

    public double getWeightInKilograms() {
        return getWeightInGrams() / 1000.0;
    }

    public boolean isStable() {
        return status == ScaleStatus.STABLE;
    }

    public enum WeightUnit {
        GRAM(1.0),
        KILOGRAM(1000.0),
        POUND(453.592),
        OUNCE(28.3495);

        private final double gramsPerUnit;

        WeightUnit(double gramsPerUnit) {
            this.gramsPerUnit = gramsPerUnit;
        }

        public double toGrams(double value) {
            return value * gramsPerUnit;
        }
    }

    public enum ScaleStatus {
        STABLE,
        UNSTABLE,
        OVERLOAD,
        UNDERLOAD,
        ERROR,
        ZERO,
        TARE
    }
}
